class MachineProduce extends eui.Component{
	constructor(){
		super();
		this.skinName="skins.scene.MachineProduceSkin";
		this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAdded, this );
		this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoved, this);
	}

	private onProduce(evt:egret.TouchEvent){
		if( this.lack.length == 0){
			window["__machineProduce"](this.curMachine.machineId,this.lastSelectItem.curData.goodsId)
		}else{
			ShowHint(this.lack.join(",")+"数量不足，请前往家族商店购买");
		}

	}
	private lastSelectItem:FuckItem;
	private onShopItem(evt:egret.Event){
		if( this.lastSelectItem){
			this.lastSelectItem.setState("normal");
		}
		this.lastSelectItem = evt.data;
		this.lastSelectItem.setState("select");
		this.updateProductItemRelated(this.lastSelectItem.goodInfo);
	}
	private lack = [];
	private updateProductItemRelated(data:GoodInfo){
		// var material = GuildMgr.getInstance().getProducableMaterials(data.id);
		// var goodInfo:GoodInfo;
		// var index = 0;
		// var item:FuckItem;
		// while( this.container_product_0.numElements){
		// 	FuckItem.recycle(<FuckItem>this.container_product_0.removeChildAt(0));
		// }
		// this.lack = [];
		// var needCnt;
		// var curCnt;
		// for( var key in material){
		// 	goodInfo = Model.GetGoodInfoById(parseInt(key));
		// 	item = FuckItem.gain();
		// 	item.x = index * 80;
		// 	item.y = 0;
		// 	this.container_product_0.addChild(item);
		// 	curCnt = GuildMgr.getInstance().getGoodsCnt(parseInt(key),0);
		// 	needCnt = material[key];
		// 	item.setData(goodInfo,4);
		// 	this["lab_m_"+index].text = curCnt+"/"+ needCnt;
		// 	if( curCnt < needCnt){
		// 		this.lack.push(goodInfo.name);
		// 	}
		// 	index++;
		// }
		// for(; index <3 ; index++){
		// 	this["lab_m_"+index].text =  "";
		// }
		// this.item_target.setData(this.lastSelectItem.curData.goodsId,2);
		// if(this.currentProducable.indexOf(this.lastSelectItem.goodInfo.id) != -1){
		// 	this.img_produce.visible =true;
		// 	this.lab_1.text = this.item_target.goodInfo.name+"\r"+ this.item_target.goodInfo.descs+"\r制作时间:"+this.lastSelectItem.curData.costMins+"分钟";
		// }else{
		// 	this.img_produce.visible =false;
		// 	this.lab_1.text = this.item_target.goodInfo.name+"\r"+ this.item_target.goodInfo.descs+"\r机器"+
		// 		GuildMgr.getInstance().getProuctById(this.lastSelectItem.goodInfo.id).machineLevel+"级解锁";
		// }
	}

	private onAdded(evt:egret.Event){
		EventController.getInstance().addEventListener(EventController.PRODUCT_ITEM_SELECTED,this.onShopItem, this);
		this.img_left.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onLeft, this);
		this.img_right.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRight, this);
		this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
		this.img_produce.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onProduce, this);

	}

	private onRemoved(evt:egret.Event){
		EventController.getInstance().removeEventListener(EventController.PRODUCT_ITEM_SELECTED,this.onShopItem, this);
		this.img_left.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onLeft, this);
		this.img_right.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onRight, this);
		this.img_close.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
		this.img_produce.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onProduce, this);
	}

	private onClose(evt:egret.TouchEvent){
		SoundMgr.getInstance().playEffect(SoundMgr.BTN);
		this.hide();
	}

	public hide(){
		DisplayController.instance().removeFromAlert(GameConst.GrayMask);
		DisplayController.instance().removeFromAlert(this);
	}

	private curMachine;
	public show(machineId){
		// this.curMachine = GuildMgr.getInstance().machines[machineId];
		// if( !DisplayController.instance().stage) return;
		// DisplayController.instance().addToAlert(GameConst.GrayMask);
		// DisplayController.instance().addToAlert(this);
		// if(this.maxPage == 0)
		// 	this.maxPage = Math.ceil(GuildMgr.getInstance().allGoods.length / 6);

		this.updateDisplay();
	}
	private curPage = 0;
	private maxPage = 0;
	private updateDisplay(){
		this.curPage = 0;
		this.displayPage();
	}
	private currentProducable:number[];
	private displayPage(){
		// this.currentProducable = GuildMgr.getInstance().getProducableGoodByLevel(this.curMachine.level);
		// while( this.container_product.numElements){
		// 	FuckItem.recycle(<FuckItem>this.container_product.removeElementAt(0));
		// }
		// var startIndex = 6*this.curPage;
		// var goods = GuildMgr.getInstance().allGoods;
		// var len = goods.length;
		// var item:FuckItem;
		// for(var i = 0; i < 6; i++){
		// 	if(startIndex+i < len){
		// 		item = FuckItem.gain();
		// 		item.x = (i%3) * 80;
		// 		item.y = Math.floor(i/3)*80;
		// 		this.container_product.addChild(item);
		// 		item.setData(goods[startIndex+i],2);
		// 		item.img_lock.visible = this.currentProducable.indexOf(item.goodInfo.id) == -1;
		// 	}else{
		// 		break;
		// 	}
		// }
		// if( this.container_product.numElements){
		// 	item = <FuckItem>this.container_product.getElementAt(0);
		// 	this.lastSelectItem = item;
		// 	this.lastSelectItem.setState("select");
		// 	this.updateProductItemRelated(item.goodInfo);
		// }
		this.img_left.visible = this.curPage > 0;
		this.img_right.visible = this.curPage < this.maxPage - 1 ;
	}
	private onRight(evt:egret.TouchEvent){
		SoundMgr.getInstance().playEffect(SoundMgr.BTN);
		if( this.curPage < this.maxPage-1){
			this.curPage++;
			this.displayPage();
		}
	}
	private onLeft(evt:egret.TouchEvent){
		SoundMgr.getInstance().playEffect(SoundMgr.BTN);
		if( this.curPage > 0){
			this.curPage--;
			this.displayPage();
		}
	}

	public img_close:eui.Image;
	public item_target:FuckItem;
	public item_m_0:FuckItem;
	public item_m_1:FuckItem;
	public item_m_2:FuckItem;
	public img_left:eui.Image;
	public img_right:eui.Image;
	public img_produce:eui.Image;
	public lab_1:eui.Label;
	public lab_m_0:eui.Label;
	public lab_m_1:eui.Label;
	public lab_m_2:eui.Label;
	public container_product:eui.Group;
	public container_product_0:eui.Group;
	public partAdded(name:string, instance:any){
		super.partAdded(name, instance);
		if(name == "img_close"){
			this.img_close=instance;
		}else if(name == "img_produce"){
			this.img_produce=instance;
		}else if(name == "item_target"){
			this.item_target=instance;
		}else if(name == "item_m_0"){
			this.item_m_0=instance;
		}else if(name == "item_m_1"){
			this.item_m_1=instance;
		}else if(name == "item_m_2"){
			this.item_m_2=instance;
		}else if(name == "img_left"){
			this.img_left=instance;
		}else if(name == "img_right"){
			this.img_right=instance;
		}else if(name == "lab_1"){
			this.lab_1=instance;
		}else if(name == "lab_m_0"){
			this.lab_m_0=instance;
		}else if(name == "lab_m_1"){
			this.lab_m_1=instance;
		}else if(name == "lab_m_2"){
			this.lab_m_2=instance;
		}else if(name == "container_product"){
			this.container_product=instance;
		}else if(name == "container_product_0"){
			this.container_product_0=instance;
		}
	}
	public childrenCreated(){
		super.childrenCreated();
	}

	public measure(){
		super.measure();
		this.x = GameConst.StageW - this.measuredWidth >> 1;
		this.y = GameConst.StageH - this.measuredHeight >> 1;
	}

	private static _instance:MachineProduce;
	public static getInstance():MachineProduce{
		if(MachineProduce._instance == null){
			MachineProduce._instance = new MachineProduce();
		}
		return MachineProduce._instance;
	}
}